package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.Area;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.AreaService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2015/8/30.
 */
@Controller("apiAreaController")
@RequestMapping("/area")
public class AreaController extends BaseController {

    @Resource(name = "areaServiceImpl")
    private AreaService areaService;

    /**
     * 选取地区
     */
    @ResponseBody
    @RequestMapping(value = "/getJsonArea", method = RequestMethod.GET)
    public Object getJsonArea(Long parentId) {
        if (parentId != null) {
            final Area parent=areaService.find(parentId);
            final List<Area> areas= areaService.getCacheListData("o.parent.id=?1", new Object[]{parentId});
            return Message.createMessage(Message.success("success"), new HashMap<String, Object>() {{
                put("parent", parent);
                put("list", areas);
            }});
        } else {
            final List<Area> areas=  areaService.getCacheListData(" o.parent is null",new Object[]{});
            return Message.createMessage(Message.success("success"), new HashMap<String, Object>() {{
                put("list", areas);
            }});
        }
    }

    /**
     * 选取地区
     */
    @RequestMapping(value = "/getArea", method = RequestMethod.GET)
    public String getArea(Long parentId, ModelMap model) {
        if (parentId != null) {
            Area parent=areaService.find(parentId);
            model.addAttribute("parent", parent);
            model.addAttribute("areas", areaService.getCacheListData("o.parent.id=?1", new Object[]{parentId}));
        } else {
            model.addAttribute("areas", areaService.getCacheListData(" o.parent is null", new Object[]{}));
        }
        return "/area/getArea";
    }



}
